package com.raincat.tools.multimediapickertools;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.raincat.tools.multimediapickertools.crop.CropView;
import com.raincat.tools.multimediapickertools.utils.EasyActionBar;
import com.raincat.tools.multimediapickertools.utils.ImageCacheUtils;
import com.raincat.tools.multimediapickertools.utils.MultiMediaController;
import com.raincat.tools.multimediapickertools.utils.MultiMediaItem;
import com.raincat.tools.R;

/**
 * <pre>
 *     author : RainCat
 *     org    : Shenzhen JingYu Network Technology Co., Ltd.
 *     e-mail : nining377@gmail.com
 *     time   : 2019/02/27
 *     desc   : 图片裁剪
 *     version: 1.0
 * </pre>
 */

public class CropActivity extends AppCompatActivity {
    private Context context;

    private CropView cropView;
    private Button btn_cancel, btn_sure;

    private MultiMediaItem multiMediaItem;
    private ProgressDialog dialog;
    private ImageCacheUtils imageCacheUtils;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
        localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        setTheme(MultiMediaController.themeId);
        setContentView(R.layout.picker_activity_crop);
        context = CropActivity.this;

        for (MultiMediaItem multiMediaItem : MultiMediaController.multiMediaItemMap.values()) {
            this.multiMediaItem = multiMediaItem;
        }

        if (multiMediaItem == null)
            finish();

        init();
        setData();
        listener();
    }

    private void init() {
        EasyActionBar easyActionBar = findViewById(R.id.easyActionBar);
        easyActionBar.setImmerseLayout();
        easyActionBar.setTitleRes(-1);
        easyActionBar.setTitleText(getString(R.string.picker_crop));
        cropView = findViewById(R.id.cropView);
        btn_cancel = findViewById(R.id.btn_cancel);
        btn_sure = findViewById(R.id.btn_sure);
    }

    private void setData() {
        Uri source = Uri.parse("file://" + multiMediaItem.path);
        cropView.of(source).withAspect(MultiMediaController.outputX, MultiMediaController.outputY).withOutputSize(MultiMediaController.outputX, MultiMediaController.outputY).initialize(context);
    }

    private void listener() {
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(context, null, getString(R.string.picker_now_loading_please_wait));

                new Thread() {
                    public void run() {
                        final Bitmap croppedBitmap = cropView.getOutput();
                        final String key = System.currentTimeMillis() + "";
                        imageCacheUtils = new ImageCacheUtils(context);
                        imageCacheUtils.addBitmapToFile(key, 100, croppedBitmap);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialog.dismiss();
                                if (croppedBitmap == null)
                                    Toast.makeText(context, getString(R.string.picker_not_supported), Toast.LENGTH_SHORT).show();
                                else {
                                    MultiMediaController.multiMediaItemMap.clear();
                                    multiMediaItem.zoomPath = imageCacheUtils.getCachePath(key);
                                    MultiMediaController.multiMediaItemMap.put(multiMediaItem.path, multiMediaItem);
                                    setResult(RESULT_OK);
                                }
                                finish();
                            }
                        });
                    }
                }.start();
            }
        });
    }
}